public class demo4 {
    //力扣 203. 移除链表元素
    public ListNode removeElements(ListNode head, int val) {
        ListNode cut = head;
        if (head == null) {
            return null;
        }
        while (cut != null) {

            if (cut.next != null && cut.next.val == val) {
                if (cut.next.next == null) {
                    cut.next = null;
                } else {
                    cut.next = cut.next.next;
                }
            } else {
                cut = cut.next;
            }
        }
        //使用这个判断的前题是head！=null   把这个条件判断了就不会报错了
        if (head.val == val) {
            head = head.next;
        }
        return head;
    }
}
